package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.core.db.dao.ext.PermissionResourceDao;
import com.zhuss.core.db.entity.PermissionResource;

/**
 * 查询权限、资源关系 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("permissionResourceRead")
public class PermissionResourceRead {
	
	@Autowired
	private PermissionResourceDao permissionResourceDao;
	
	/**
	 * type
	 * 根据权限查询资源列表
	 * @param permissionId
	 * @return
	 */
	public Object listResourceId(Long permissionId, Integer type) {
		List<PermissionResource> resourceList = permissionResourceDao.listResourceByType(Arrays.asList(permissionId), type);
		List<Long> resourceIdList = new ArrayList<Long>();
		transferResource(resourceList, resourceIdList);
		return resourceIdList;
	}
	
	/**
	 * 根据权限的列表  查询所关联的资源列表ID
	 * @param permissionId
	 * @return
	 */
	public Object listResourceId(List<Long> permissionIdList) {
		List<Long> result = permissionResourceDao.listResourceId(permissionIdList);
		return result;
	}
	
	private void transferResource(List<PermissionResource> resourceList,
			List<Long> resourceIdList) {
		for (PermissionResource item : resourceList) {
			resourceIdList.add(item.getResourceId());
		}
		
	}
	
	/**
	 * 根据资源查询权限列表
	 * @param resourceIdList
	 * @return
	 */
	public List<Long> listPermissionId(List<Long> resourceIdList) {
		List<PermissionResource> permissionList = new ArrayList<PermissionResource>();
		for (Long resourceId : resourceIdList) {
			permissionList.addAll(permissionResourceDao.listPermission(resourceId));
			
		}
		List<Long> permissionIdList = new ArrayList<Long>();
		transferPermission(permissionList, permissionIdList);
		return permissionIdList;
	}
	

	/**
	 * 根据资源查询权限列表
	 * @param permissionId
	 * @return
	 */
	public Object listPermissionId(Long resourceId) {
		List<PermissionResource> permissionList = permissionResourceDao.listPermission(resourceId);
		List<Long> permissionIdList = new ArrayList<Long>();
		transferPermission(permissionList, permissionIdList);
		return permissionIdList;
	}

	private void transferPermission(List<PermissionResource> permissionList,
			List<Long> permissionIdList) {
		for (PermissionResource item : permissionList) {
			permissionIdList.add(item.getPermissionId());
		}
		
	}
	
	

}
